/*! @file parsethread.cpp
 * @brief Implements ParseThread Class
 * @date 10.10.2012
 */

#include "parsethread.h"

#include <QDebug>

/**
 * @brief ctor
 *
 * @fn ParseThread::ParseThread
 */
ParseThread::ParseThread(QFile *f, BinaryViewWidget *ptr)
{
    qDebug() << "ParseThread\t\t| started.";

    file = f;
    bvw_pointer = ptr;
    emptylinecount = 0;
    bytesperline = bvw_pointer->spinBox->value();
    bvw_pointer->byteLabel->setText("Working ...");
    bvw_pointer->lineLabel->setText("Working ...");
    bvw_pointer->filesizeLabel->setText("Working ...");
    bvw_pointer->parsetimeLabel->setText("Working ...");
    bvw_pointer->emptyLineLabel->setText(QString("Gefilterte Zeilen: %1").arg(QString::number(emptylinecount)));
    filterEmpty = bvw_pointer->filterEmptyLinesCheckBox->isChecked();
    //bvw_pointer->cancelPushButton->setEnabled(true);
}

/**
 * @brief dtor
 *
 * @fn ParseThread::~ParseThread
 */
ParseThread::~ParseThread()
{
    //bvw_pointer->cancelPushButton->setEnabled(false);
    Q_EMIT finished();
    qDebug() << "ParseThread\t\t| finished.";
}

/**
 * @brief parse file
 *
 * @fn ParseThread::process
 */
int ParseThread::process() {
    qDebug() << "ParseThread\t\t| process() called.";

    QString asciibuffer;
    QString hexbuffer;
    QString finalbuffer;
    bytecount = 0;
    linecount = 0;
    emptylinecount = 0;
    empty = true;
    if(blob.isEmpty())
        blob = file->read(/*2048*/file->size());

    hexbuffer += (QString("%1:\t ").arg(bytecount, 10, 16, QChar('0')).toUpper());
    while (bytecount < blob.size()) {
        // line hex in buffer
        hexbuffer += QString(blob.at(bytecount)).toLatin1().toHex().toUpper() + " ";
        // line ascii in buffer
        if(QChar(blob.at(bytecount)).isPrint()) {
            if(QChar(blob.at(bytecount)) < 32
                    || QChar(blob.at(bytecount)) > 255)
                asciibuffer += ".";
            else {
                asciibuffer += QChar(blob.at(bytecount));
                empty = false;
            }
        }
        else
            asciibuffer += ".";
        // new line
        if((bytecount > 0) && (bytecount % bytesperline == 0)) {
            if (!empty) {
                finalbuffer += hexbuffer + "  ";
                finalbuffer += asciibuffer;
                finalbuffer += "\n";
                if ((bytecount % 1000) == 0)
                    Q_EMIT progressSetValue(bytecount);
                asciibuffer.clear();
                hexbuffer.clear();
                finalbuffer += (QString("%1:\t ").arg(bytecount, 10, 16, QChar('0')).toUpper());
                linecount++;
                empty = true;
            }
            else if (empty && filterEmpty){
                finalbuffer += "\n";
                if ((bytecount % 1000) == 0)
                    Q_EMIT progressSetValue(bytecount);
                asciibuffer.clear();
                hexbuffer.clear();
                finalbuffer += (QString("%1:\t ").arg(bytecount, 10, 16, QChar('0')).toUpper());
                linecount++;
                emptylinecount++;
                empty = true;
            }
            empty = true;
        }
        bytecount++;
    }

    asciibuffer.clear();

    qDebug() << "ParseThread\t\t| process() emitting writeMe().";
    Q_EMIT writeMe(finalbuffer);

    bvw_pointer->lineLabel->setText(QString::number(linecount));
    bvw_pointer->byteLabel->setText(QString::number(bytecount));
    bvw_pointer->filesizeLabel->setText(QString::number(file->size()/1024) + " kB");
    bvw_pointer->emptyLineLabel->setText(QString("Gefilterte Zeilen: %1").arg(QString::number(emptylinecount)));
    bytecount = 0;

    qDebug() << "ParseThread\t\t| parseFile() finished. Size:" << file->size();

    return 0;
}
